WTL Controls
Back to the WTL Controls Home Page.
BIS_WTL_CONTROLS::CComboBoxPlus Class Reference

A CComboBox descendant providing customizable background and text colors for both the edit and list box portions of the control. More...

#include <ComboBoxPlus.h>

Inheritance diagram for BIS_WTL_CONTROLS::CComboBoxPlus:

Public Member Functions

 CComboBoxPlus () noexcept
 
virtual ~CComboBoxPlus ()
 
void SubclassComboBox (HWND hWnd)
 Subclass a combobox control and create GDI brushes. More...
 
void UnSubclassComboBox ()
 Un-subclass main and component windows; free GDI resources. More...
 
void SetControlBackColor (COLORREF value)
 Sets a new background color for the control portion of the combobx when it doesn't have focus. More...
 
COLORREF GetControlBackColor () const
 Default: COLOR_WINDOW. More...
 
void SetControlTextColor (COLORREF value)
 Set a new text color for the control portion of the combobox when it doesn't have focus. More...
 
COLORREF GetControlTextColor () const
 Default: COLOR_WINDOWTEXT. More...
 
void SetControlFocusedBackColor (COLORREF value)
 Set a new background color for the control portion of the combobox when it has focus. More...
 
COLORREF GetControlFocusedBackColor () const
 Default: COLOR_WINDOW. More...
 
void SetControlFocusedTextColor (COLORREF value)
 Set a new text color for the control portion of the combobox when it has focus. More...
 
COLORREF GetControlFocusedTextColor () const
 Default: CLR_RED. More...
 
void SetListBoxBackColor (COLORREF value)
 Set a new background color for the listbox portion of the combobox. More...
 
COLORREF GetListBoxBackColor () const
 Default: COLOR_WINDOW. More...
 
void SetListBoxTextColor (COLORREF value)
 Set a new text color for the listbox portion of the combobox. More...
 
COLORREF GetListBoxTextColor () const
 Default: COLOR_WINDOWTEXT. More...
 
void SetAutoAdjustDropRectWidth (bool value)
 Change the AutoAdjustRectWidth state. More...
 
bool GetAutoAdjustDropRectWidth () const
 Default: true. More...
 
void SetMaxDropDownWidth (int value)
 Sets the upper limit to the width of the dropdown list, if it is being automatically adjusted. More...
 
int GetMaxDropDownWidth () const
 Default: 400 pixels. More...
 
void SetReadOnly (bool read_only=true)
 Change the read-only state of the combobox. More...
 
bool GetReadOnly () const
 Default: false. More...
 
void UpdateDropDownWidth ()
 Force an update of the width of the drop down list. More...
 

Detailed Description

A CComboBox descendant providing customizable background and text colors for both the edit and list box portions of the control.

This class handles WM_CTLCOLOR variants to provide custom background and text colors for the edit and listbox portions of a combo box. It even allows for different colors for focused vs. non-focused states.

This class also provides automatic adjustment of the width of the dropdown portion of the combobox. A MaxDropDownWidth property limits the maximum width, while the minimum width is limited to the width of the static or edit control component. Within these limits, the drop down width will adjust itself to accomodate the longest string in the list.

The parent dialog must reflect WM_CTLCOLOREDIT messages to the control. Use either the non-discriminating REFLECT_NOTIFICATIONS_EX, or with something more selective (e.g, REFLECT_NOTIFICATIONS_MSG_FILTERED).

NOTE: In Windows 10, the custom coloring of the control portion of a combobox only works with 'simple' (created with the CBS_SIMPLE style flag) comboboxes or 'drop down' (created with the CBS_DROPDOWN) comboboxes. Custom coloring of drop-list comboboxes (those created with the CBS_DROPLIST style flag) via WM_CTLCOLOR messages is limited to the listbox portion of the control. In the past, drop-list boxes sent WM_CTLCOLORSTATIC messages; in effect, in read-only comboboxes, the edit control was replaced with a static control, and it was possible to manipulate it with WM_CTLCOLORSTATIC. In Windows 10, that is no longer the case. It appears that drop list boxes no longer send WM_CTLCOLORSTATIC messages; instead, they send WM_CTLCOLOREDIT messages like edit-able comboboxes do, but any modifications made to the control's appearance in response to WM_CTLCOLOREDIT are ignored.

If you need a drop-list combobox with fully customizable colors, use a CODComboxBox (the owner-drawn alternative to CComboBoxPlus).

HOWEVER! All that being said, there is a way to approximate the behavior of a fully owner-drawn, drop-list combobox while still employing the WM_CTLCOLOR technique of customizing the appearance - simply make the edit portion of a drop down combobox read-only. Aside from some minor differences (e.g., a context menu that doesn't exist in true drop-list comboboxes), it looks and works like a fully customized, owner-drawn drop-list combob box.

Typical usage is depicted below.

CComboBoxPlus m_Combo;
m_Combo.SubclassComboBox(GetDlgItem(IDC_COMBO1));
m_Combo3.SetListBoxBackColor(CLR_RED);
m_Combo.SetControlFocusedBackColor(CLR_ORANGE);
m_Combo3.SetControlTextColor(CLR_DARK_BLUE);
m_Combo3.SetControlBackColor(CLR_GREEN);
m_Combo3.SetItemHeight(-1, 40);
m_Combo3.SetItemHeight(0, 40);
.
.
m_Combo.UnSubclassComboBox(); // when finished using the control (probably in OnEndDialog).

Definition at line 70 of file ComboBoxPlus.h.

Constructor & Destructor Documentation

◆ CComboBoxPlus()

BIS_WTL_CONTROLS::CComboBoxPlus::CComboBoxPlus ( )
noexcept

Definition at line 21 of file ComboBoxPlus.cpp.

◆ ~CComboBoxPlus()

BIS_WTL_CONTROLS::CComboBoxPlus::~CComboBoxPlus ( )
virtual

Definition at line 46 of file ComboBoxPlus.cpp.

Member Function Documentation

◆ SubclassComboBox()

void BIS_WTL_CONTROLS::CComboBoxPlus::SubclassComboBox ( HWND  hWnd)

Subclass a combobox control and create GDI brushes.

This also grabs the window handles of the list box and control portions of the combobox. Note that the control portion can be either a static text control or a edit control, depending on whether or not the contents of the combobox can be edited.

Parameters
hWndThe window handle of the control to subclass.
Return values
void

Definition at line 67 of file ComboBoxPlus.cpp.

◆ UnSubclassComboBox()

void BIS_WTL_CONTROLS::CComboBoxPlus::UnSubclassComboBox ( )

Un-subclass main and component windows; free GDI resources.

Definition at line 96 of file ComboBoxPlus.cpp.

◆ SetControlBackColor()

void BIS_WTL_CONTROLS::CComboBoxPlus::SetControlBackColor ( COLORREF  value)

Sets a new background color for the control portion of the combobx when it doesn't have focus.

Parameters
valueNew control background color.
Return values
void

Definition at line 257 of file ComboBoxPlus.cpp.

◆ GetControlBackColor()

COLORREF BIS_WTL_CONTROLS::CComboBoxPlus::GetControlBackColor ( ) const

Default: COLOR_WINDOW.

Returns the background color of the control portion of the combobox when it doesn't have focus.

Return values
COLORREFCurrent non-focused control background color.

Definition at line 243 of file ComboBoxPlus.cpp.

◆ SetControlTextColor()

void BIS_WTL_CONTROLS::CComboBoxPlus::SetControlTextColor ( COLORREF  value)

Set a new text color for the control portion of the combobox when it doesn't have focus.

Parameters
valueNew control text color.
Return values
void

Definition at line 289 of file ComboBoxPlus.cpp.

◆ GetControlTextColor()

COLORREF BIS_WTL_CONTROLS::CComboBoxPlus::GetControlTextColor ( ) const

Default: COLOR_WINDOWTEXT.

Returns the text color used in the control portion of the combobox when it doesn't have focus.

Return values
COLORREFCurrent non-focused control text color.

Definition at line 275 of file ComboBoxPlus.cpp.

◆ SetControlFocusedBackColor()

void BIS_WTL_CONTROLS::CComboBoxPlus::SetControlFocusedBackColor ( COLORREF  value)

Set a new background color for the control portion of the combobox when it has focus.

Parameters
valueNew focused control background color.
Return values
void

Definition at line 317 of file ComboBoxPlus.cpp.

◆ GetControlFocusedBackColor()

COLORREF BIS_WTL_CONTROLS::CComboBoxPlus::GetControlFocusedBackColor ( ) const

Default: COLOR_WINDOW.

Returns the background color of the control portion of the combobox when it has focus.

Return values
COLORREFCurrent focused control background color.

Definition at line 303 of file ComboBoxPlus.cpp.

◆ SetControlFocusedTextColor()

void BIS_WTL_CONTROLS::CComboBoxPlus::SetControlFocusedTextColor ( COLORREF  value)

Set a new text color for the control portion of the combobox when it has focus.

Parameters
valueNew focused control text color.
Return values
void

Definition at line 350 of file ComboBoxPlus.cpp.

◆ GetControlFocusedTextColor()

COLORREF BIS_WTL_CONTROLS::CComboBoxPlus::GetControlFocusedTextColor ( ) const

Default: CLR_RED.

Returns the text color for the control portion when it has focus.

Return values
COLORREFCurrent focused control text color.

Definition at line 335 of file ComboBoxPlus.cpp.

◆ SetListBoxBackColor()

void BIS_WTL_CONTROLS::CComboBoxPlus::SetListBoxBackColor ( COLORREF  value)

Set a new background color for the listbox portion of the combobox.

Parameters
valueThe new background color for the listbox portion of the combobox.
Return values
void

Definition at line 378 of file ComboBoxPlus.cpp.

◆ GetListBoxBackColor()

COLORREF BIS_WTL_CONTROLS::CComboBoxPlus::GetListBoxBackColor ( ) const

Default: COLOR_WINDOW.

Returns the color used for the background of the listbox portion of the combobox.

Return values
COLORREFCurrent listbox background color.

Definition at line 364 of file ComboBoxPlus.cpp.

◆ SetListBoxTextColor()

void BIS_WTL_CONTROLS::CComboBoxPlus::SetListBoxTextColor ( COLORREF  value)

Set a new text color for the listbox portion of the combobox.

Parameters
valueNew listbox text color.
Return values
void

Definition at line 410 of file ComboBoxPlus.cpp.

◆ GetListBoxTextColor()

COLORREF BIS_WTL_CONTROLS::CComboBoxPlus::GetListBoxTextColor ( ) const

Default: COLOR_WINDOWTEXT.

Returns the color used for text in the listbox portion of the combobox.

Return values
COLORREFCurrent listbox text color.

Definition at line 396 of file ComboBoxPlus.cpp.

◆ SetAutoAdjustDropRectWidth()

void BIS_WTL_CONTROLS::CComboBoxPlus::SetAutoAdjustDropRectWidth ( bool  value)

Change the AutoAdjustRectWidth state.

Parameters
valueNew bool state.
Return values
void

Definition at line 199 of file ComboBoxPlus.cpp.

◆ GetAutoAdjustDropRectWidth()

bool BIS_WTL_CONTROLS::CComboBoxPlus::GetAutoAdjustDropRectWidth ( ) const

Default: true.

If true, the drop box width is adjusted to accommodate the longest string. The width will not be less than the control width, and will not be greater than the max limit set through SetMaxDropDownWidth. if false, drop down width will be equal to the control width.

Return values
bool

Definition at line 184 of file ComboBoxPlus.cpp.

◆ SetMaxDropDownWidth()

void BIS_WTL_CONTROLS::CComboBoxPlus::SetMaxDropDownWidth ( int  value)

Sets the upper limit to the width of the dropdown list, if it is being automatically adjusted.

Parameters
valueNew maximum, in pixels.
Return values
void

Definition at line 228 of file ComboBoxPlus.cpp.

◆ GetMaxDropDownWidth()

int BIS_WTL_CONTROLS::CComboBoxPlus::GetMaxDropDownWidth ( ) const

Default: 400 pixels.

The maximum width allowed when AutoAdjustDropRectWidth is true.

Return values
int

Definition at line 214 of file ComboBoxPlus.cpp.

◆ SetReadOnly()

void BIS_WTL_CONTROLS::CComboBoxPlus::SetReadOnly ( bool  value = true)

Change the read-only state of the combobox.

If set true, and if the combobox is of the 'simple' or 'drop down' type, the edit portion of the combobox will be made read-only, approximating the appearance and behavior of a fully owner-drawn drop list combobox.

Parameters
valueThe new read-only state.
Return values
void

Definition at line 440 of file ComboBoxPlus.cpp.

◆ GetReadOnly()

bool BIS_WTL_CONTROLS::CComboBoxPlus::GetReadOnly ( ) const

Default: false.

Returns the current ReadOnly bool state.

Return values
bool

Definition at line 423 of file ComboBoxPlus.cpp.

◆ UpdateDropDownWidth()

void BIS_WTL_CONTROLS::CComboBoxPlus::UpdateDropDownWidth ( )

Force an update of the width of the drop down list.

If AutoAdjustWidth is true, the drop-down width will be set equal to the length of the longest string in the list box, within limits: if the length of the longest string is less than the width of the control portion of the combobox, the width will be set to the width of the control portion, and if the width of the longest string is greater than the MaxDropDownWidth value, the width will be set to MaxDropDownWidth.

If AutoAdjustWidth is /c false, the width of the dropdown list will be set equal to the width of the control portion of the combobox.

Return values
void

Definition at line 124 of file ComboBoxPlus.cpp.


The documentation for this class was generated from the following files: